<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic\calendar;


use app\common\model\calendar\Calendar;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * Calendar逻辑
 * Class CalendarLogic
 * @package app\adminapi\logic
 */
class CalendarLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/05/28 14:11
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            Calendar::create([
                'platform' => $params['platform'],
                'title' => $params['title'],
                'description' => $params['description'],
                'start_date' => $params['start_date'],
                'end_date' => $params['end_date'],
                'remind_minutes' => $params['remind_minutes'],
                'rrule' => $params['rrule']
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }

    /**
     * @notes 复制莫忘日历
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/05/28 14:11
     */
    public static function copyPlatformCalendar($platform): bool
    {
        $calendar = Calendar::where('id',1)->findOrEmpty();
        Calendar::create([
            'platform' => $platform,
            'title' => $calendar->title??'',
            'description' => $calendar->description??'',
            'start_date' => $calendar->start_date??0,
            'end_date' => $calendar->end_date??0,
            'remind_minutes' => $calendar->remind_minutes??'',
            'rrule' => $calendar->rrule??'FREQ=DAILY;UNTIL=20261231T140000Z'
        ]);
        return true;
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/05/28 14:11
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            Calendar::where('id', $params['id'])->update([
                'platform' => $params['platform'],
                'title' => $params['title'],
                'description' => $params['description'],
                'start_date' => strtotime($params['start_date']),
                'end_date' => strtotime($params['end_date']),
                'remind_minutes' => $params['remind_minutes'],
                'rrule' => $params['rrule']
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/05/28 14:11
     */
    public static function delete(array $params): bool
    {
        return Calendar::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/05/28 14:11
     */
    public static function detail($params): array
    {
        $calendar = Calendar::where('platform',$params['platform'])->findOrEmpty();
        if (!$calendar->isEmpty()){
            $calendar['remind_minutes'] = explode(",", $calendar['remind_minutes']);
            $calendar['start_date'] = date('Y-m-d H:i:s',$calendar['start_date']);
            $calendar['end_date'] =  date('Y-m-d H:i:s',$calendar['end_date']);
        }
        return $calendar->toArray();
    }
}